package com.bruip.controller;

import com.bruip.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.Oneway;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王若龙
 * @CreateTime 2022-09-06 18:44
 * <p>
     * 1. @RequestBody 将请求体中的内容和控制器方法的形参绑定
 * 2. 使用@RequestBody注解将json转换为java对象
 * a>导入jackson的依赖
 * b>在SpringMVC中设置  <mvc:annotation-driven/>
 * c>在处理请求的控制器方法的形参位置，直接设置json格式的请求参数要转换的java类型的形参，使用@RequestBody注解标识即可
 * 3. @ResponseBody：将所标识的控制器方法的返回值作为响应报文的响应体响应到浏览器
 * 4. 使用@ResponseBody注解响应浏览器json格式的数据
 * a>导入jackson的依赖
 * b>在SpringMVC中设置  <mvc:annotation-driven/>
 * c>将需要转换为json字符串的java对象直接作为控制器方法的返回值，@ResponseBody注解标识控制器方法就可以将java对象直接转换为json对象
 *
 *
 */
@Controller
public class TestAjaxController {

    @RequestMapping("/test/ajax")
    public void testAjax(Integer id, @RequestBody String requestBody, HttpServletResponse response) throws IOException {
        System.out.println("requestBody = " + requestBody);
        System.out.println("id = " + id);
        response.getWriter().write("hello axios");
    }

    @RequestMapping("/test/RequestBody/json")
    public void testRequestBody(@RequestBody Map<String, Object> map, HttpServletResponse response) throws IOException {
        System.out.println("user = " + map);
        response.getWriter().write("hello requestBody");
    }

    public void testRequestBody(@RequestBody User user, HttpServletResponse response) throws IOException {
        System.out.println("user = " + user);
        response.getWriter().write("hello requestBody");
    }

    @RequestMapping("/test/Response")
    @ResponseBody
    public String testResponse() {
        return "success";
    }

    @RequestMapping("test/responseBody/json")
    @ResponseBody
    public List<User> testResponseBody(){
        User user1 = new User(1001, "admin", "123456", 23, "男");
        User user2 = new User(1002, "admin", "123456", 23, "男");
        User user3 = new User(1003, "admin", "123456", 23, "男");
        User user4 = new User(1004, "admin", "123456", 23, "男");
        return Arrays.asList(user1, user2, user3, user4);
    }

//    public Map<Integer, Object> testResponseBody(){
//        User user1 = new User(1001, "admin", "123456", 23, "男");
//        User user2 = new User(1002, "admin", "123456", 23, "男");
//        User user3 = new User(1003, "admin", "123456", 23, "男");
//        User user4 = new User(1004, "admin", "123456", 23, "男");
//        Map<Integer, Object> map = new HashMap<>();
//        map.put(user1.getId(), user1);
//        map.put(user2.getId(), user2);
//        map.put(user3.getId(), user3);
//        map.put(user4.getId(), user4);
//        return map;
//    }
//    public User testResponseBody(){
//        return new User(1001, "admin", "123456", 23, "男");
//    }
}
